class AdminController < ApplicationController
  scaffold :category,:suffix=>true
  scaffold :comment,:suffix=>true
  scaffold :image,:suffix=>true
  
  #before_filter :index, :only => [:new_category,:new_comment,:new_image]
  # 
  #  before_filter :save_image, :only=>:create_image
  #before_filter :update_img, :only=>:update_image
  PER_PAGE = 10
  
  def create_image
    @image = Image.new(params[:image])
    @image.define_urls
    if @image.save
      @image.create_files
      flash[:notice] = "Image was successfully added"
      redirect_to :action => "list_image"
    else
      render :action=>'new_image'
    end
  end
  
  def update_image
    @image = Image.find(params[:id])
    @image_param= params[:image]
    @image.delete_files('full') if @image_param['large_image'].inspect !~ %r{.+StringIO*.}i && !@image_param['large_image'].original_filename.empty?
    @image.delete_files('thumb') if @image_param['thumbnail'].inspect !~ %r{.+StringIO*.}i && !@image_param['thumbnail'].original_filename.empty?
    if @image.update_attributes(params[:image])
      if @image.large_image.inspect !~ %r{.+StringIO*.}i && !@image.large_image.original_filename.empty?
        @image.define_urls('full')
        @image.update_file('full')
      end
      if @image.thumbnail.inspect !~ %r{.+StringIO*.}i && !@image.thumbnail.original_filename.empty?
        #raise
        @image.define_urls('thumb')
        @image.update_file('thumb')
      end
      flash[:notice] = 'Image was successfully updated.'
      redirect_to :action => 'list_image'
    else
      render :action => 'edit_image'
    end 
  end
  
  def delete_image
    Image.find(params[:id]).destroy
    redirect_to :action => "list_image"
  end
  
  def index
    list_image
  end
  
  def find_by_category
    category=params[:category]
    @images = Category.find(category[:cur_id]).images
    render :partial=>'out_image_list',:layout=>false    
  end
  
  def search    
    keyword=params[:search][:keyword]
    if !keyword.empty?
      
      keyword=keyword.gsub(/\s/,"+.*")
      query=""
      if params[:search][:brief_descr] == "1"
        query+="or" if query !=""
        query+="(brief_descr rlike '.*#{keyword}+.*') "         
      end
      if params[:search][:full_descr] == "1"
        query+="or" if query !=""
        query+="(full_descr rlike '.*#{keyword}+.*') "          
      end
      if params[:search][:name] == "1"
        query+="or" if query !=""
        query+="(name rlike '.*#{keyword}+.*')"       
      end
      if params[:search][:author] == "1"
        query+="or" if query !=""
        query+="(author rlike '.*#{keyword}+.*')"       
      end    
      
      @image_pages, @images = paginate(:images,:order_by=>'sequence',:conditions=>query, :per_page => PER_PAGE)
      
      keyword=params[:search][:keyword]
      keyword=keyword.split
      keyword.each{|key| 
        patern=Regexp.new(key,Regexp::IGNORECASE)
        @images.each{|image|
          image.brief_descr.sub!(patern){|str|
            str.insert(0,"<font class='hightlight'>")
            str.insert(-1,"</font>")
          }
          image.full_descr.sub!(patern){|str|
            str.insert(0,"<font class='hightlight'>")
            str.insert(-1,"</font>")            
          }
          image.name.sub!(patern){|str|
            str.insert(0,"<font class='hightlight'>")
            str.insert(-1,"</font>")
          }
          image.author.sub!(patern){|str|
            str.insert(0,"<font class='hightlight'>")
            str.insert(-1,"</font>")
          }
        }
      }
    else
      @image_pages, @images = paginate(:images,:order_by=>'sequence', :per_page => PER_PAGE)
    end
    @categories = Category.find(:all,:order=>'sequence')
    @cur_category= ""
    
    render 'admin/list_image'
  end
end
